<?php
namespace app\admin\controller;
use app\admin\model\Advertise;
use think\Cache;
use think\Db;

/**
 * 广告管理
 * Class Member
 */
class AdvertiseController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
        $ad_list = db('advertise')->where($map)->order($order)->paginate(15, false, get_query());
        return $this->fetch('index', ['ad_list' => $ad_list, 'keyword' => $keyword]);
    }
	
    /**
     * 添加广告
     * @return mixed
     */
    public function add() {
    	$url = config('DOMAIN')."/index/index/adver";
		$this->assign('url', $url);
        return $this->fetch();
    }

    /**
     * 保存广告
     */
    public function save() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			$data['add_time'] = now_time();
			$ads_model = new Advertise();
            if ($ads_model->allowField(true)->save($data)) {
                $this->success('添加成功',url('index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑广告
     * @param $id
     * @return mixed
     */
    public function edit($id) {
    	$ads_model = new Advertise();
        $ads = $ads_model->find($id);
        return $this->fetch('edit', ['ads' => $ads]);
    }

    /**
     * 更新广告
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			$ads_model = new Advertise();
            if ($ads_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功',url('index'));
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除广告
	public function delete_item(){
		$res = del_data("advertise",input("id"));
		if($res){
			 $this->success('删除成功',url("manage"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	/**
     * 分享配置
     */
    public function sharesz() {
		if ($this->request->isPost()) {
            $ticket_config   = $this->request->post('share_config/a');
            $data['value'] = serialize($ticket_config);
            if (Db::name('system')->where('name', 'share_config')->update($data) !== false) {
                Cache::set('share_config', $ticket_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('share_config')) {
	            $ticket_config = Cache::get('share_config');
	        } else {
	            $ticket_config = Db::name('system')->field('value')->where('name', 'share_config')->find();
	            $ticket_config = unserialize($ticket_config['value']);
	        }
        	return $this->fetch('sharesz', ['share_config' => $ticket_config]);
        }
    }
    
	//夺宝攻略
	public function setadver(){
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'adver_config')->update($data) !== false) {
                Cache::set('gl_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('adver_config')) {
	            $site_config = Cache::get('adver_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'adver_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('setadver', ['adver_config' => $site_config]);
        }
		return $this->fetch();
	}
}
